<template>
  <div class="ucan-ticket" @click="handleClick">
    <img :src="ticketBackground" class="ucan-ticket-bg">
    <!-- <img src="https://img.alicdn.com/tfs/TB15gSFIHvpK1RjSZFqXXcXUVXa-200-50.png" class="ucan-ticket-logo" alt=""> -->
    <share-icon class="share-icon" v-if="forwardable && !disabled" />
    <div class="ucan-ticket-name">
      {{type}}<span class="ucan-ticket-user-name">
        | {{name}}
      </span>
    </div>
    <div class="ucan-ticket-message">
      <p>{{activeTimeStr}}</p>
      <p>地址: {{address || '-'}}</p>
      <p>票价：{{price || '-'}}元</p>
    </div>
    <div v-if="!disabled">
      <div class="ucan-ticket-footer" v-if="appointment">
        4月25日预约截止
        <co-button class="ucan-ticket-action" @click.stop="preserve()" color="primary">预约分会场</co-button>
      </div>
      <div class="ucan-ticket-footer" v-if="!appointment">
        <co-button class="ucan-ticket-action" color="outline" @click.stop="goForwardPage(activityId, id)">
          转赠
        </co-button>
        <co-button class="ucan-ticket-action" color="primary" @click.stop="openTokenDialog">
          凭证
        </co-button>
      </div>
    </div>
    <div class="ucan-ticket-disabled" v-if="disabled">
      <div class="ucan-ticket-status">{{disabledText}}</div>
    </div>
    <co-dialog :open="open" class="ticket-token-dialog-wrapper" dialog-class="ticket-token-dialog">
      <div class="ticket-token-dialog-close" @click="closeTokenDialog">
        <co-icon-close />
      </div>
      <div class="ticket-token-dialog-title">
        凭证
      </div>
      <div class="ticket-token-dialog-qrcode">
        <img v-if="qrcode" :src="qrcode" />
      </div>
      <div class="ticket-token-dialog-desc">
        二维码每分钟更新截屏无效
      </div>
    </co-dialog>
  </div>
</template>
<script>
import jrQrcode from "jr-qrcode";
import shareIcon from "../forward/share-icon";
import services from "../../api/mine";
import { addZero } from "../../utils";

export default {
  name: "UcanTicket",
  components: {
    shareIcon
  },
  props: {
    id: [String, Number],
    activityId: [String, Number],
    type: String,
    name: String,
    address: String,
    price: [String, Number],
    activeTime: {
      type: Object,
      default: () => ({
        stime: new Date().getTime(),
        etime: new Date().getTime(),
        activeDate: ""
      })
    },
    forwardable: Boolean,
    appointment: Boolean,
    forwarded: Boolean,
    expired: Boolean,
    used: Boolean
  },
  computed: {
    disabled() {
      return this.forwarded || this.expired || this.used;
    },
    ticketBackground() {
      return this.disabled
        ? "https://gw.alicdn.com/tfs/TB1o4XcKQvoK1RjSZFDXXXY3pXa-690-336.png"
        : "https://gw.alicdn.com/tfs/TB1b_wTHkvoK1RjSZFDXXXY3pXa-1380-912.png";
    },
    disabledText() {
      switch (true) {
        case this.forwarded:
          return "已转送";
        case this.used:
          return "已使用";
        case this.expired:
          return "已过期";
      }
      return "";
    },
    activeTimeStr() {
      const { stime, etime, activeDate } = this.activeTime;
      const start = new Date(stime || undefined);
      const end = new Date(etime || undefined);

      return `${addZero(start.getMonth() + 1)}月${addZero(
        start.getDate()
      )}日~${addZero(end.getMonth() + 1)}月${addZero(
        end.getDate()
      )}日 ${activeDate}`;
    },
    deadline() {
      if (!this.deadlineTime) return "";
      const date = new Date(this.deadlineTime);
      return `${addZero(date.getMonth() + 1)}月${addZero(date.getDate())}日`;
    }
  },
  data() {
    return {
      open: false,
      qrcode: "",
      deadlineTime: null
    };
  },
  mounted() {
    console.log("mounted", this.id, this.$route.query.ticketId);
    services.getDeadlineTime(this.activityId).then(({ cutoffTime }) => {
      this.deadlineTime = Number(cutoffTime);
    });
  },
  methods: {
    handleClick(e) {
      if (this.disabled) return;
      this.$emit("click", e);
    },
    goForwardPage(activityId, ticketId) {
      console.log(activityId, ticketId, this.id);

      this.$router.push({
        path: "/forward/give",
        query: {
          activityId,
          ticketId
        }
      });
    },
    preserve() {
      this.$router.push(`/preserve/${this.activityId}`);
    },
    openTokenDialog() {
      this.open = true;
      services.getQRCode(this.activityId).then(({ qrcode }) => {
        this.qrcode = jrQrcode.getQrBase64(qrcode, {
          width: 356,
          height: 356
        });
      });
    },

    closeTokenDialog() {
      this.open = false;
    }
  }
};
</script>
<style lang="less">
.ucan-ticket {
  padding: 24px 32px 0;
  position: relative;
  z-index: 1;
  text-align: left;
  color: #fff;
  .ucan-ticket-logo {
    width: 200px;
    margin-bottom: 32px;
    margin-left: 8px;
  }
  + .ucan-ticket {
    margin-top: 20px;
  }

  .share-icon {
    position: absolute;
    right: 24px;
    top: 24px;
  }
  .ucan-ticket-bg {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
  }

  .ucan-ticket-name {
    font-size: 44px;
    padding-left: 8px;
  }

  .ucan-ticket-user-name {
    font-size: 30px;
  }

  .ucan-ticket-message {
    font-size: 20px;
    margin-top: 16px;
    padding-bottom: 32px;
    padding-left: 8px;
    > p {
      margin-top: 8px;
      &:first-child {
        margin-top: 0;
      }
    }
  }

  .ucan-ticket-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 20px;
    color: fade(#eee, 70%);
    padding-top: 28px;
    padding-bottom: 24px;
    border-top: 1px dotted #979797;
    &.only-action {
      justify-content: center;
    }
  }

  .ucan-ticket-action {
    width: 280px;
    height: 72px;
    font-size: 30px;
  }

  .ucan-ticket-disabled {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.2);
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .ucan-ticket-status {
    background: url(https://gw.alicdn.com/tfs/TB1LeATHbrpK1RjSZTEXXcWAVXa-286-286.png)
      no-repeat;
    background-size: 100%;
    width: 143px;
    height: 143px;
    padding-top: 36px;
    text-align: center;
    font-size: 28px;
    color: #1a1b1f;
    position: relative;
    left: 11%;
  }
}

.ticket-token-dialog-wrapper {
  padding: 0 32px;
}

.ticket-token-dialog {
  width: 100%;
  border-radius: 4px;
  height: 760px;
  position: relative;
  .co-dialog-body {
    padding-top: 68px;
  }
}

.ticket-token-dialog-title {
  font-size: 34px;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 52px;
}

.ticket-token-dialog-qrcode {
  width: 356px;
  height: 356px;
  margin: 0 auto;
  > img {
    width: 100%;
    height: 100%;
    display: block;
  }
}

.ticket-token-dialog-desc {
  text-align: center;
  font-size: 30px;
  color: #333;
  margin-top: 60px;
}

.ticket-token-dialog-close {
  background: #d8d8d8;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 30px;
  color: #35383f;
  position: absolute;
  bottom: -120px;
  left: 50%;
  margin-left: -40px;
}
</style>

